//
//  HomeTableViewController.swift
//  swiftImitationWeibo
//
//  Created by 杨果果 on 16/7/9.
//  Copyright © 2016年 yang. All rights reserved.
//

import UIKit
import SVProgressHUD
private let ID = "Identifier"
class HomeTableViewController: VisitorTableViewController {
    
    //设置模型
    private lazy var statuesListViewModel: StatuesListViewModel = StatuesListViewModel()
    override func viewDidLoad() {
        super.viewDidLoad()
        if isLogin {
            setupUI()
            setuTableView()
            loadStautes()
        }else{
            vistorView?.updataVisitorViewInfo(nil, imageName: nil)
        }
    }
    private func setupUI() {
        guard let nav = self.navigationController else {
            return
        }
        nav.view.insertSubview(tipLabel, aboveSubview: nav.navigationBar)
        tipLabel.frame = CGRect(x: 0, y: 34, width: nav.navigationBar.width, height: 30)
    }
    
    //懒加载下拉控件
    private lazy var pullUpView: UIActivityIndicatorView = {
       let indicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        indicatorView.color = UIColor.redColor()
        return indicatorView
    }()

    //下拉刷新
    //使用系统的
    private lazy var pullDownView: RefreshControl = {
        let ctr = RefreshControl()
        ctr.addTarget(self, action: "pullDownRefresh", forControlEvents: .ValueChanged)
        return ctr
    }()
    
    //加载数据
    @objc private func pullDownRefresh() {
        loadStautes()
        
    }
    //结束刷新
    private func endRefreshing() {
        pullUpView.stopAnimating()
        pullDownView.endRefreshing()
    }
    
    private lazy var tipLabel: UILabel = {
        let label = UILabel(textColor: UIColor.whiteColor(), fontSize: 12)
        label.textAlignment = .Center
        label.hidden = true
        return label
    }()
    //开启tip 动画
    private func startAnimation(message: String) {
        //如果在执行动画则不需要再次执行
        if tipLabel.hidden == false {
            return
        }
        tipLabel.hidden = false
        tipLabel.text = message
        UIView.animateWithDuration(1, animations: { () -> Void in
            self.tipLabel.transform = CGAffineTransformMakeTranslation(0, self.tipLabel.height)
            }) { (_) -> Void in
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.tipLabel.transform = CGAffineTransformIdentity
                    }, completion: { (_) -> Void in
                        self.tipLabel.hidden = true
                })
        }
    }
    
    private func loadStautes() {
//        SVProgressHUD.show()
        statuesListViewModel.loadStatus(pullUpView.isAnimating()) { (isSuccess, message) -> () in
//            SVProgressHUD.dismiss()
            
            if !self.pullUpView.isAnimating() {
                self.startAnimation(message)
            }
            
            self.endRefreshing()
            if isSuccess {
                self.tableView.reloadData()
            }
        }
    }
    
    private func setuTableView() {
        //注册cell
        tableView.registerClass(HomeTableViewCell.self, forCellReuseIdentifier: ID)
        tableView.separatorStyle = .None
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = UITableViewAutomaticDimension
        //添加自定义的下拉刷新
        tableView.addSubview(pullDownView)
        //添加footView
        tableView.tableFooterView = pullUpView
        pullUpView.sizeToFit()
    }
    
    
    //tableview dataSource
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(ID, forIndexPath: indexPath) as! HomeTableViewCell
        //设置没有选中cell时的颜色
        cell.selectionStyle = .None
        let statusViewModel = statuesListViewModel.statusList[indexPath.row]
        cell.statueViewModel = statusViewModel
        
        return cell
    }
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return statuesListViewModel.statusList.count
    }
    
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if indexPath.row == statuesListViewModel.statusList.count - 1 && pullUpView.isAnimating() {
            pullUpView.startAnimating()
            print("滚动到最后一个cell")
            loadStautes()
        }
    }
    
    
    
    
}
